<?php

namespace App\Services;

use App\Models\UserProperty;
use common\utils\SysCode;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\SafeCenter;
use App\Models\User;
use common\sms\SMSManager;
use Illuminate\Support\Facades\Auth;
use common\utils\RSACrypt;

//帮助公告service
class SafeCenterService
{
    // 验证有效时间, 默认5分钟
    const VALIDATE_AVAILABLE_TIME = 5;

    // 认证通过KEY
    const PASS_KEY = 'pass';

    //安全中心设置
    const SET_SAFETY =
        [
            'id_card' => ['status' => '0', 'value' => '认证成功后，将无法修改身份信息，且账户资金仅能提现到该认证人本人的银行卡中，请谨慎填写。'],
            'password' => ['status' => '1', 'value' => '为保证您的账户安全，建议您定期更改密码以保护账户安全。', 'reg' => '^[~!@#$%^&*()-_=+|{},.?:;a-zA-Z0-9]{6,16}$'],
            'withdraw_password' => ['status' => '0', 'value' => '在进行提款操作时通过提款密码进行身份验证，为您资产账户安全加把锁。', 'reg' => '^[0-9]{4}$'],
            'mobile' => ['status' => '0', 'value' => '您的手机号还没验证，为了保障账户安全，请及时验证！'],
            'email' => ['status' => '0', 'value' => '您的邮箱还没验证，为了保障账户安全，请及时验证。'],
            'google_auth' => ['status' => '0', 'value' => '申请谷歌身份验证之后，可通过谷歌身份验证码登录账号，确保您的账户更安全。']
        ];


    public static function getSafeCenter($attrs)
    {
        return SafeCenter::where($attrs)->first();
    }

    public static function addSafeCenter($id)
    {
        $userInfo = UserService::getBaseinfo($id);
        $safeCenter = new SafeCenter();
        $safeCenter['user_id'] = $id;
        $safeCenter['mobile'] = $userInfo['mobile'] ? 1 : 0;
        $safeCenter['email'] = $userInfo['email'] ? 1 : 0;
        if (!$safeCenter->save()) {
            return error_exit(SysCode::INVALID_ARGUMENT);
        }
    }

    /**
     * 生成code
     */
    public static function getValidateCode($id, $key, $m = self::VALIDATE_AVAILABLE_TIME)
    {
        $code = mt_rand(1000, 9999);
        self::setValidateCode($id, $key, $code, $m);
        return $code;
    }

    /**
     * 设置code
     */
    public static function setValidateCode($id, $key, $code, $m = self::VALIDATE_AVAILABLE_TIME)
    {
        $key = RedisKeys::USER_VERIFY . $id . ':' . $key;
        Redis::set($key, $code, 'EX', $m * 60);
    }

    /**
     * 校验code
     */
    public static function validateCode($id, $key, $code, $isDel = true)
    {
        if (empty($id) || empty($key) || empty($code)) {
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        $key = RedisKeys::USER_VERIFY . $id . ':' . $key;
        $VCode = Redis::get($key);
        if ($VCode != $code) {
            error_exit(SysCode::INVALID_CODE);
        }
        if ($isDel) {
            Redis::del($key);
        }
        return true;
    }

    /**
     * 验证通过，生成唯一值存入redis
     */
    public static function setUniqueCode($id)
    {
        $code = uniqueCode();
        self::setValidateCode($id, self::PASS_KEY, $code);
        return $code;
    }

    public static function checkUniqueCode($id, $code, $isDel = false)
    {
        return self::validateCode($id, self::PASS_KEY, $code, $isDel);
    }

    /**
     * 发送邮件验证码
     */
    public static function sendEmailCode($id, $email)
    {
        $code = self::getValidateCode($id, $email);
        // 发送邮件
        MailService::sendValidMail($email, $code);
        return true;
    }

    /**
     * 验证邮件验证码
     */
    public static function validateEmailCode($id, $email, $code)
    {
        return self::validateCode($id, $email, $code);
    }

    /**
     * 发送手机验证码
     */
    public static function sendMobileCode($id, $mobile)
    {
        $code = self::getValidateCode($id, $mobile, self::VALIDATE_AVAILABLE_TIME);
        // 发送短信
        SMSManager::sendValidMsg($mobile, $code, self::VALIDATE_AVAILABLE_TIME);
        $re = json_decode(SMSManager::getErrorMsg());
        if($re->code == 1){
            return true;
        }else{
            return $re->msg;
        }
    }

    /**
     * 验证手机验证码
     */
    public static function validateMobileCode($id, $mobile, $code)
    {
        return self::validateCode($id, $mobile, $code);
    }
}
